package com.miyake.loveviewweb.client.dialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;

public class UploadDialog extends Composite implements IDialogPanel {
    
    private HorizontalPanel panel = new HorizontalPanel();
    
    public UploadDialog(String defaultName, String action) {
        panel.setWidth("100%");
        initWidget(panel);
        initialize(defaultName, action);
    }

    private FileUpload fileUpload = new FileUpload();
    private FormPanel formPanel = new FormPanel();
    private DialogHandler dialogHandler;
//    private UploadDialogHandler handler = null;
    
    private void initialize(String defaultName, final String action) {       
        panel.add(formPanel);
        formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
        formPanel.setMethod(FormPanel.METHOD_POST);
        formPanel.add(fileUpload);

        formPanel.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {

            public void onSubmitComplete(SubmitCompleteEvent event) {
                if (dialogHandler != null) {
                    dialogHandler.onOK();
                }
/*                if (handler != null) {
                    handler.onCompelte();                    
                }*/
            }
            
        });
        formPanel.addSubmitHandler(new SubmitHandler() {

            public void onSubmit(SubmitEvent event) {

               // Window.alert("Submit");
            }
            
        });
        
        fileUpload.setName("fileupload");
        
        panel.add(new Button("Upload", new ClickHandler() {
            public void onClick(ClickEvent event) {
                upload(action);
            }
        }));
        
    }
    
    public void upload(String action) {
        formPanel.setAction(action/*"upload"*/);
        formPanel.submit();
    }

/*    *//**
     * @return the handler
     *//*
    public UploadDialogHandler getHandler() {
        return handler;
    }

    *//**
     * @param handler the handler to set
     *//*
    public void setHandler(UploadDialogHandler handler) {
        this.handler = handler;
    }*/

    public void setDialogHandler(DialogHandler handler) {
        this.dialogHandler = handler;
    }

    public Composite getComposite() {
        return this;
    }
}
